package com.jyx.manage_media_process.mq;

import com.alibaba.fastjson.JSON;
import com.jyx.framework.domain.media.MediaFile;
import com.jyx.framework.domain.media.MediaFileProcess_m3u8;
import com.jyx.framework.utils.HlsVideoUtil;
import com.jyx.framework.utils.Mp4VideoUtil;

import com.jyx.manage_media_process.config.RabbitMQConfig;
import com.jyx.manage_media_process.dao.MediaFileRepository;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 接收视频处理的消息
 */
@Component
public class MediaProcessTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(MediaProcessTask.class);
    @Value("${jyx-service-manage-media.ffmpeg-path}")
    String ffmpeg_path;
    @Value("${jyx-service-manage-media.upload-location}")
    String serverPath;
    @Autowired
    MediaFileRepository mediaFileRepository;

    /**
     * 接收视频处理的请求
     * @param msg
     * @throws IOException
     */
    @RabbitListener(queues = {RabbitMQConfig.MEDIA_QUEUE}/*,containerFactory = "customContainerFactory"*/)
    public void receiveMediaProcessTask(String msg) throws IOException{
        Map msgMap = JSON.parseObject(msg, Map.class);
        LOGGER.info("Receive media process task msg:{}",msgMap);
        String mediaId = (String) msgMap.get("mediaId");
        Optional<MediaFile> mediaFileOptional = mediaFileRepository.findById(mediaId);
        if(!mediaFileOptional.isPresent()){
            LOGGER.info("Media is not exist,{}",mediaId);
            return;
        }
        MediaFile mediaFile = mediaFileOptional.get();
        //1.先判断是否需要处理
        String fileType = mediaFile.getFileType();
        if(fileType == null || !fileType.equals("avi")){
            mediaFile.setProcessStatus("303004");  //设置状态为无需处理
            mediaFileRepository.save(mediaFile);
        }else {
            mediaFile.setProcessStatus("303001");  //设置状态为未处理
            mediaFileRepository.save(mediaFile);
        }
        //2.生成MP4
        String video_path = serverPath+mediaFile.getFilePath()+mediaFile.getFileName();
        String mp4_name = mediaId+".mp4";
        String mp4Folder_path = serverPath + mediaFile.getFilePath();
        Mp4VideoUtil mp4VideoUtil = new Mp4VideoUtil(ffmpeg_path,video_path,mp4_name,mp4Folder_path);
        String result = mp4VideoUtil.generateMp4();
        //处理失败把信息写入MediaFile
        if(result == null || !result.equals("success")){
            mediaFile.setProcessStatus("303003"); //处理失败代码
            MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
            mediaFileProcess_m3u8.setErrormsg(result);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
            mediaFileRepository.save(mediaFile);
            LOGGER.error("Change video to mp4 failed,file id is:{}",mediaFile.getId());
            return;
        }
       /* //生成m3u8
        //1.设置路径
        video_path = serverPath + mediaFile.getFilePath() + mp4_name;
        String m3u8_name = mediaId + ".m3u8";
        String m3u8_path = serverPath + mediaFile.getFilePath() + "hls/";
        HlsVideoUtil hlsVideoUtil = new HlsVideoUtil(ffmpeg_path, video_path, m3u8_name, m3u8_path);
        result = hlsVideoUtil.generateM3u8();
        if(result == null || !result.equals("success")){
            mediaFile.setProcessStatus("303003"); //处理失败代码
            MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
            mediaFileProcess_m3u8.setErrormsg(result);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
            mediaFileRepository.save(mediaFile);
            LOGGER.error("Change video to m3u8 failed,file id is:{}",mediaFile.getId());
            return;
        }
        //2.生成ts
        List<String> ts_list = hlsVideoUtil.get_ts_list();
        //3.设置MediaFileProcess_m3u8类
        MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
        mediaFileProcess_m3u8.setTslist(ts_list);
        mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);*/
        //4.返回
        mediaFile.setProcessStatus("303002"); //处理成功代码
        mediaFile.setFileUrl(mediaFile.getFilePath()+mp4_name);
        mediaFileRepository.save(mediaFile);
    }

}
